/*
Author: Surpass-HC
Source: https://gitee.com/hxchuan000/clpc-1
Mail: surpass-hc@qq.com
*/
#include <clogFile.h>
#include <fileOpt.h>
int clogFileCfgFsize(struct clogOutFileInf *fileInf, int cfgval)
{
    if (!fileInf)
    {
        printf("[%s,%d]\n", __FILE__, __LINE__);
        return -1;
    }
    fileInf->logOneSize = cfgval;
    return 1;
}
int clogFileCfgFnum(struct clogOutFileInf *fileInf, int cfgval)
{
    if (!fileInf)
    {
        printf("[%s,%d]\n", __FILE__, __LINE__);
        return -1;
    }
    fileInf->logFiles = cfgval;
    return 1;
}
int clogFileCfgFlush(struct clogOutFileInf *fileInf, int cfgval)
{
    if (!fileInf)
    {
        printf("[%s,%d]\n", __FILE__, __LINE__);
        return -1;
    }
    fileInf->logFlush = cfgval;
    return 1;
}
int clogFileCfgFname(struct clogOutFileInf *fileInf, const char *cfgval)
{
    if (!fileInf)
    {
        printf("[%s,%d]\n", __FILE__, __LINE__);
        return -1;
    }
    fileInf->logBaseName = cfgval;
    return 1;
}
int clogOutFileInit(struct clogOutFileInf *fileInf)
{
    if (!fileInf)
    {
        printf("[%s,%d]\n", __FILE__, __LINE__);
        return -1;
    }
    memset(fileInf, 0, sizeof(struct clogOutFileInf));
    return 1;
}
int clogOutFileQuit(struct clogOutFileInf *fileInf)
{
    if (fileInf->logFilePtr)
    {
        fclose(fileInf->logFilePtr);
        fileInf->logFilePtr = NULL;
    }
    memset(fileInf, 0, sizeof(struct clogOutFileInf));
    return 1;
}
static int clogOutFileOpen(struct clogOutFileInf *fileInf)
{
    if (!fileInf)
    {
        printf("[%s,%d]\n", __FILE__, __LINE__);
        return -1;
    }
    if (fileInf->logBaseName == NULL)
    {
        printf("[%s,%d],open need file\n", __FILE__, __LINE__);
        return -1;
    }
    char tmpfile0[512];
    snprintf(tmpfile0, sizeof(tmpfile0), "%s.c.log.000", fileInf->logBaseName);
    fileInf->logFilePtr = fopen(tmpfile0, "a+");
    if (!fileInf->logFilePtr)
    {
        printf("[%s,%d],open file err\n", __FILE__, __LINE__);
        return -1;
    }
    return 1;
}
static int clogOutFileChk(struct clogOutFileInf *fileInf)
{
    if (!fileInf)
    {
        printf("[%s,%d]\n", __FILE__, __LINE__);
        return -1;
    }
    if (!fileInf->logFilePtr)
    {
        return clogOutFileOpen(fileInf);
    }
    if (ftell(fileInf->logFilePtr) < fileInf->logOneSize)
    {
        return 1;
    }
    fclose(fileInf->logFilePtr);
    fileInf->logFilePtr = NULL;
    char tmpfileOld[512];
    char tmpfileNew[512];
    for (int i = fileInf->logFiles; i > 0; i--)
    {
        snprintf(&tmpfileOld[0], sizeof(tmpfileOld), "%s.c.log.%03d", fileInf->logBaseName, i - 1);
        snprintf(&tmpfileNew[0], sizeof(tmpfileNew), "%s.c.log.%03d", fileInf->logBaseName, i);
        rename(tmpfileOld, tmpfileNew);
    }
    return clogOutFileOpen(fileInf);
}
void clogPrtFileFun(void *waydata, const char *logstr, int strlen)
{
    struct clogOutFileInf *tmpfile = (struct clogOutFileInf *)waydata;
    if (!tmpfile)
    {
        printf("[%s,%d]file ptr err\n", __FILE__, __LINE__);
        return;
    }
    if (tmpfile->logBaseName == NULL)
    {
        printf("[%s,%d],not cfg file path\n", __FILE__, __LINE__);
        return;
    }
    if (tmpfile->logOneSize <= 0)
    {
        printf("[%s,%d],not cfg file len\n", __FILE__, __LINE__);
        return;
    }
    if (tmpfile->logFiles <= 0)
    {
        printf("[%s,%d],not cfg file save\n", __FILE__, __LINE__);
        return;
    }
    if (clogOutFileChk(tmpfile) < 0)
    {
        printf("[%s,%d],log file chk fail\n", __FILE__, __LINE__);
        return;
    }
    fwrite(logstr, 1, strlen, tmpfile->logFilePtr);
    if (tmpfile->logFlush)
    {
        fflush(tmpfile->logFilePtr);
    }
}
